package com.mf.ergate.service.auth.impl;

import com.mf.ergate.api.auth.model.dto.RoleResourceRelationQueryDto;
import com.mf.ergate.api.auth.model.dto.RoleUserRelationQueryDto;
import com.mf.ergate.dao.auth.RoleInfoDao;
import com.mf.ergate.domain.model.auth.RoleInfoEntity;
import com.mf.ergate.domain.model.auth.RoleResourceRelationEntity;
import com.mf.ergate.domain.model.auth.RoleUserRelationEntity;
import com.mf.ergate.service.GenericService;
import com.mf.ergate.service.auth.RoleInfoService;
import com.mf.ergate.service.auth.RoleResourceRelationService;
import com.mf.ergate.service.auth.RoleUserRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色信息Service层实现类
 * User: zhaoming
 * DateTime: 2019-02-08
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class RoleInfoServiceImpl extends GenericService<RoleInfoEntity> implements RoleInfoService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleInfoDao roleInfoDao;
    @Autowired
    private RoleUserRelationService roleUserRelationService;
    @Autowired
    private RoleResourceRelationService roleResourceRelationService;

    /**
     * 删除角色及相关信息(逻辑删除)
     * @param roleInfoEntity 角色信息Entity
     * @return 执行结果
     */
    @Transactional
    public Boolean deleteCompleteByUniqueNo(RoleInfoEntity roleInfoEntity){
        //删除用户与角色关联信息
        RoleUserRelationQueryDto roleUserRelationQueryDto = new RoleUserRelationQueryDto();
        roleUserRelationQueryDto.setRoleNo(roleInfoEntity.getUniqueNo());
        List<RoleUserRelationEntity> roleUserRelationEntities = roleUserRelationService.selectByCondition(roleUserRelationQueryDto);
        if (CollectionUtils.isNotEmpty(roleUserRelationEntities)) {
            for (RoleUserRelationEntity data : roleUserRelationEntities) {
                data.setUpdateUserNo(roleInfoEntity.getUpdateUserNo());
                data.setUpdateUserName(roleInfoEntity.getUpdateUserName());
                data.setUpdateTime(roleInfoEntity.getUpdateTime());
            }
            roleUserRelationService.deleteByUniqueNos(roleUserRelationEntities);
        }
        //删除角色与资源关联信息
        RoleResourceRelationQueryDto roleResourceRelationQueryDto = new RoleResourceRelationQueryDto();
        roleResourceRelationQueryDto.setRoleNo(roleInfoEntity.getUniqueNo());
        List<RoleResourceRelationEntity> roleResourceRelationEntities = roleResourceRelationService.selectByCondition(roleResourceRelationQueryDto);
        if (CollectionUtils.isNotEmpty(roleResourceRelationEntities)) {
            for (RoleResourceRelationEntity data : roleResourceRelationEntities) {
                data.setUpdateUserNo(roleInfoEntity.getUpdateUserNo());
                data.setUpdateUserName(roleInfoEntity.getUpdateUserName());
                data.setUpdateTime(roleInfoEntity.getUpdateTime());
            }
            roleResourceRelationService.deleteByUniqueNos(roleResourceRelationEntities);
        }
        //删除角色信息
        return roleInfoDao.deleteByUniqueNo(roleInfoEntity);
    }
}
